<?php

class history{
	
	// Define variables
	private $ID_HISTORY;
	private $ACCOUNT_ORIGIN;
	private $ACCOUNT_DESTINY;
	private $TRANSACTION_TYPE_ID;
	private $CURRENCY_ID;
	private $AMOUNT;
	private $DATE;
	
	// Connection with database and instance creation
	private $DB;
	public function __construct() {
		$config = config::getInstance();
		$this->DB = $config->config_values['database']['db_prefix'];
	}
	
	// Getters and setters functions
  	public function setID_HISTORY($value) {
  		$this->ID_HISTORY = $value;
  	}
	public function getID_HISTORY() {
  		return $this->ID_HISTORY;
  	}
	public function setACCOUNT_ORIGIN($value) {
  		$this->ACCOUNT_ORIGIN = $value;
  	}
	public function getACCOUNT_ORIGIN() {
  		return $this->ACCOUNT_ORIGIN;
  	}
	public function setACCOUNT_DESTINY($value) {
  		$this->ACCOUNT_DESTINY = $value;
  	}
	public function getACCOUNT_DESTINY() {
  		return $this->ACCOUNT_DESTINY;
  	}
	public function setTRANSACTION_TYPE_ID($value) {
  		$this->TRANSACTION_TYPE_ID = $value;
  	}
	public function getTRANSACTION_TYPE_ID() {
  		return $this->TRANSACTION_TYPE_ID;
  	}
	public function setCURRENCY_ID($value) {
  		$this->CURRENCY_ID = $value;
  	}
	public function getCURRENCY_ID() {
  		return $this->CURRENCY_ID;
  	}
	public function setAMOUNT($value) {
  		$this->AMOUNT = $value;
  	}
	public function getAMOUNT() {
  		return $this->AMOUNT;
  	}
	public function setDATE($value) {
  		$this->DATE = $value;
  	}
	public function getDATE() {
  		return $this->DATE;
  	}

	// General functions:
  	public function load($id)
	{
		$db = db::getInstance();
		$sql = "SELECT * FROM ".$this->DB."history WHERE ID_HISTORY=".$id;

		foreach ($db->query($sql) as $row) {
			$this->setID_HISTORY($row['ID_HISTORY']);
			$this->setACCOUNT_ORIGIN($row['ACCOUNT_ORIGIN']);
			$this->setACCOUNT_DESTINY($row['ACCOUNT_DESTINY']);
			$this->setTRANSACTION_TYPE_ID($row['TRANSACTION_TYPE_ID']);
			$this->setCURRENCY_ID($row['CURRENCY_ID']);
			$this->setAMOUNT($row['AMOUNT']);
			$this->setDATE($row['DATE']);
		}
	    $db = null; 
	    return true;			
  	}

  	public function insert(){
    	try {
			$db = db::getInstance();
			$id = $this->maxId();
			$sql = "INSERT INTO ".$this->DB."history (";
			$sql .=" ID_HISTORY";
			$sql .=",ACCOUNT_ORIGIN";
			$sql .=",ACCOUNT_DESTINY";
			$sql .=",TRANSACTION_TYPE_ID";
			$sql .=",CURRENCY_ID";
			$sql .=",AMOUNT";
			$sql .=",DATE";
			$sql .=") VALUES (";
	  		$sql .=$id;
	  		$sql .=",'".$this->getACCOUNT_ORIGIN()."'";
	  		$sql .=",'".$this->getACCOUNT_DESTINY()."'";
	  		$sql .=",".$this->getTRANSACTION_TYPE_ID();
	  		$sql .=",".$this->getCURRENCY_ID();
	  		$sql .=",".$this->getAMOUNT();
	  		$sql .=", CURRENT_TIMESTAMP";
	  		$sql .=")";
	    	$sth=$db->prepare($sql);
			$sth->execute();
		    $db = null; 			
	  		return $id;
  		} catch (Exception $e) {
  			return 0;
  		}
  	}

  	public function modify($id){}

  	public function delete($id){}
  	
	public function maxId() {
		try {
			$db = db::getInstance();
			$sql = "SELECT MAX(ID_HISTORY) FROM ".$this->DB."history";
	    	$reg = 1;
	    	foreach ($db->query($sql) as $row) {
				if ($row[0]!=0) {
					$reg = $row[0] + 1;
				}
			}
			$db = null; 
	    	return $reg;
		} catch (Exception $e) {
  			return 0;
		}
    }
    
// JSON function:
	public function jsonList($acc_num){
		try {
    		$db = db::getInstance();
			$sql = "SELECT * FROM ".$this->DB."history ";
			$sql.= "JOIN ".$this->DB."transaction_type ON TRANSACTION_TYPE_ID = ID_TRANSACTION_TYPE ";
			$sql.= " WHERE ACCOUNT_ORIGIN='".$acc_num."'";
			$sql.= " OR ACCOUNT_DESTINY='".$acc_num."'";
			$sql.= " ORDER BY DATE DESC";
			$sth=$db->prepare($sql);
			$sth->execute();
			$recordset = array();
			$recordset = $sth->fetchAll(PDO::FETCH_ASSOC);
			return $recordset;
		} catch (Exception $e) {
  			return false;
		}		
	}
  	
}
?>